package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdUserRoungDamage;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdUserRoundDamageDaoImpl extends TempDataObjectDao<CrossStrongholdUserRoungDamage> {
    public static String getCrossStrongholdUserRoungDamageSql = "select * from t_c_activity_stronghold_user_round_damage where activityId=?";

    public Map<Integer, Map<Long, CrossStrongholdUserRoungDamage>> getCrossStrongholdUserRoungDamage(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, CrossStrongholdUserRoungDamage>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossStrongholdUserRoungDamageSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdUserRoungDamage data = parse(rs);
                int round = data.getRound();
                if (!dataMap.containsKey(round)) {
                    dataMap.put(round, new ConcurrentHashMap<>());
                }
                Map<Long, CrossStrongholdUserRoungDamage> damageMap = dataMap.get(round);
                damageMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossStrongholdUserRoungDamageSql = "update t_c_activity_stronghold_user_round_damage set damage=?,userBaseInfo=? where activityId=? and unionId=? and userId=? and round=?";

    @Override
    protected boolean updateData(CrossStrongholdUserRoungDamage crossStrongholdUserRoungDamage) {
        crossStrongholdUserRoungDamage.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdUserRoungDamage.getDamage().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdUserRoungDamage.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdUserRoungDamage.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdUserRoungDamage.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdUserRoungDamage.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdUserRoungDamage.getRound()));
        if (getDb().executeNoneQuery(updateCrossStrongholdUserRoungDamageSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossStrongholdUserRoungDamageSql = "insert into t_c_activity_stronghold_user_round_damage(activityId,unionId,userId,round,damage,userBaseInfo) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossStrongholdUserRoungDamage crossStrongholdUserRoungDamage) {
        crossStrongholdUserRoungDamage.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdUserRoungDamage.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdUserRoungDamage.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdUserRoungDamage.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdUserRoungDamage.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdUserRoungDamage.getDamage().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdUserRoungDamage.getUserBaseInfo())));
        if (getDb().executeNoneQuery(insertCrossStrongholdUserRoungDamageSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossStrongholdUserRoungDamage parseData(ResultSet rs) throws SQLException {
        CrossStrongholdUserRoungDamage crossStrongholdUserRoungDamage = new CrossStrongholdUserRoungDamage();
        crossStrongholdUserRoungDamage.setActivityId(rs.getInt("activityId"));
        crossStrongholdUserRoungDamage.setUnionId(rs.getString("unionId"));
        crossStrongholdUserRoungDamage.setUserId(rs.getLong("userId"));
        crossStrongholdUserRoungDamage.setRound(rs.getInt("round"));
        crossStrongholdUserRoungDamage.setDamage(new BigInteger(rs.getString("damage")));
        crossStrongholdUserRoungDamage.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));

        return crossStrongholdUserRoungDamage;
    }

}
